package edu.lingnan.librarymanage.controller;

import edu.lingnan.librarymanage.core.base.AjaxResult;
import edu.lingnan.librarymanage.core.constant.LibraryManageConstant;
import edu.lingnan.librarymanage.core.dto.LoginUserDTO;
import edu.lingnan.librarymanage.core.dto.RegisterDTO;
import edu.lingnan.librarymanage.core.exception.UserException;
import edu.lingnan.librarymanage.core.vo.BorrowVO;
import edu.lingnan.librarymanage.core.vo.LoginUserVO;
import edu.lingnan.librarymanage.service.BorrowService;
import edu.lingnan.librarymanage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author cql
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BorrowService borrowService;

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(@RequestBody @Validated LoginUserDTO user, HttpSession session) {

        String code = (String) session.getAttribute(LibraryManageConstant.CODE);
        Long expires = (Long) session.getAttribute(LibraryManageConstant.EXPIRES);
        /**
         * 移除
         */
        session.removeAttribute(LibraryManageConstant.CODE);
        session.removeAttribute(LibraryManageConstant.EXPIRES);

        user.setRealCode(code);
        user.setExpires(expires);

        LoginUserVO loginUser = userService.login(user);
        if (loginUser == null) {
            return AjaxResult.error("登录失败");
        }
        session.setAttribute(LibraryManageConstant.LOGIN_USER, loginUser);
        return AjaxResult.success();
    }


    @GetMapping("/logout")
    public String logout(HttpSession session) {
        LoginUserVO user = (LoginUserVO) session.getAttribute(LibraryManageConstant.LOGIN_USER);
        session.invalidate();
        if (user.isAdmin()) {
            return "redirect:/login.html";
        } else {
            return "redirect:/index?category=0";
        }
    }


    @PostMapping("/register")
    @ResponseBody
    public AjaxResult register(@RequestBody @Validated RegisterDTO registerDto, HttpSession session) {
        System.out.println(registerDto);
        LoginUserVO loginUser = userService.register(registerDto);
        if (loginUser == null) {
            return AjaxResult.error("注册失败");
        }
        session.setAttribute(LibraryManageConstant.LOGIN_USER, loginUser);
        return AjaxResult.success();
    }


    @ResponseBody
    @GetMapping("/user/history")
    public AjaxResult getBorrowHistory(HttpSession session) {
        /**
         * TODO 获取用户的借阅历史记录
         */
        LoginUserVO user = (LoginUserVO) session.getAttribute(LibraryManageConstant.LOGIN_USER);
        if (user == null) {
            throw new UserException("您还未登录!请先登录再看");
        }
        List<BorrowVO> result = borrowService.getBorrowHistory(user.getUserId());

        return AjaxResult.success(result);
    }


}
